package com.itheima.consumer.listeners;

import lombok.extern.slf4j.Slf4j;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.QueueBuilder;
import org.springframework.amqp.rabbit.annotation.Exchange;

import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.Map;

@Slf4j
@Component
public class MqListener {

    /**
     * 使用注解进行 监听
     * queues 就是监听的队列
     * @param msg 收到的消息
     */
    @RabbitListener(queues = "maiQueue")
    public void listenSimpleQueue(String msg){
        log.info("get message = {}" , msg);
    }

    /**
     * 监听来自fanout交换机的信息 <br/>
     * 交换机的作用<br/>
     * 1. 接受来自publisher发送的消息<br/>
     * 2. 讲消息按照规则路由奥与之绑定的队列<br/>
     * 3. FanoutExchange 的会将消息路由到每个绑定的交换机<br/>
     * <hr/>
     * Fanout 交换机<br/>
     *
     * 消息发送到每一个与交换机绑定的队列中
     * @param msg 收到的消息
     */
    @RabbitListener(queues = "fanout.queue1")
    public void listenFanoutQueue1(String msg){
        log.info("get message = {}" , msg);
    }

    /**
     * 监听来自fanout交换机的信息
     * @param msg 收到的消息
     */
    @RabbitListener(queues = "fanout.queue2")
    public void listenFanoutQueue2(String msg){
        log.error("get message = {}" , msg);
    }


    /**
     * direct 交换机
     * 队列绑定了 direct交换机 ，且持有同一个routing key的时候才能接受到消息，否则接受不到
     * @param msg
     */
    @RabbitListener(queues = "direct.queue2")
    public void listenDirectQueue1(String msg){
        log.error("q1 get message = {}" , msg);
    }

    @RabbitListener(queues = "direct.queue1")
    public void listenDirectQueue2(String msg){
        log.error("q2 sget message = {}" , msg);
    }


    /**
     * topic 交换机 支持通配符的方式绑定routing key
     * @param msg
     */
    @RabbitListener(queues = "topic.queue1")
    public void listenTopicQueue1(String msg){
        log.error("q1 get message = {}" , msg);
    }

    @RabbitListener(queues = "topic.queue2")
    public void listenTopicQueue2(String msg){
        log.error("q2 get message = {}" , msg);
    }

    /**
     * 基于@RabbitListner 声明交换机
     * @param msg
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "new.direct.queue"),
            exchange = @Exchange(name = "new.direct.exchange",type = ExchangeTypes.DIRECT),
            key = {"red","blue"}

    ))
    public void listenerQueue(String msg){
        System.out.println("queue get information");
    }



}
